package com.example.casper.jnuexperiment2025.data;

import android.content.Context;
import android.util.Log;

import com.example.casper.jnuexperiment2025.Hello05692Activity;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class DataBank {

    public static final String SHOP_ITEMS_DATA_FILE_NAME = "shop_items.data";

    public void save(ArrayList<ShopItem> shopItems, Context context) {
        try {
            FileOutputStream fileOut = context.openFileOutput(SHOP_ITEMS_DATA_FILE_NAME, Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(shopItems);
            out.close();
            fileOut.close();
        } catch (Exception e) {
            Log.e("data save",e.toString());
        }
    }

    public ArrayList<ShopItem> load(Context context) {
        ArrayList<ShopItem> shopItems=new ArrayList<>();
        try {
            FileInputStream fileInput = context.openFileInput(SHOP_ITEMS_DATA_FILE_NAME);
            ObjectInputStream inputStream = new ObjectInputStream(fileInput);
            shopItems = (ArrayList<ShopItem>) inputStream.readObject();
            inputStream.close();
            fileInput.close();
        } catch (Exception e) {
            Log.e("data save",e.toString());
        }
        return shopItems;
    }
}
